rappyfandomcom-20200215-history
Templates
Templates are a fundamental part of MediaWiki's software that allow for common information to easily be transcluded (included) into other pages. They are mainly used to add the same information to multiple pages without having to copy/paste it each time. Templates, by default, reside in the Template namespace (#10). Why Use Templates? Templates allow for consistency on a wiki. They ensure that the information they add looks the same on every page. Templates can be used to easily add a large amount of information that is used frequently or as shortcuts to easily format information that is used often across the wiki. Templates can update multiple pages at once by simply changing the template. Creating Templates To create a template, create a new page with the "Template:" prefix followed by the name of the template. This places the page in the template namespace. An example template pagename could be Template:Example. Then add the content to that page that will be transcluded elsewhere. Using Templates Templates are much like links in that it's easy to add one to a page. Simply use the wiki markup on the page and your template is added. For our example above, Template:Example, that would be . When viewing a page with a template link, MediaWiki's software fetches (also known as a template call) the content from the template page and displays it as if the content was already on the page. Controlling Template Content Templates are powerful in that they allow the transclusion of an entire page onto another. By default, everything on a template page is included where a template is used. However, template pages usually have categories of their own or documentation to explain how to use them. This information is not needed or wanted on the page where it will be transcluded. MediaWiki has 3 tags that will allow us to control what information is and isn't seen when using templates. These three tags are , and . Templates will accept any number of these tags, but will override the other two. The three of them sound similar, so lets find out how they are different. does just what it says. Any information between tags will not be included on the page where the template is used. Typically, this tag is used to stop categories and documentation from showing up when using the template. and These two tags sound the same, but are quite different. and cannot be used on the same page. If they are, the tags are ignored. Both of these tags will only be transcluded on the page where the template is used. The difference is: will include its information on the transcluded page but will not show its information on the template page. This is helpful to hide the results of the template on the template page and only show the template documentation. will only include what is between these tags where the template is used and will still show this information on the template page. Parameters Another powerful feature of templates is the use of parameters. A parameter can be a word or a phrase and it is passed along to the template for evaluation. They can be named or unnamed (more on that below). Parameters can be added to the template call using the | (pipe) character. All templates can accept parameters, but unless the template is configured to use them, they are ignored. Adding Parameters To add an parameter to a template, simply add a pipe after the template name followed by the parameter. For example, . This tells MediaWiki to call the template named example with two unnamed parameters (first and second). These unnamed parameters are assigned a code within } (triple braces). Essentially, it is how the template references the parameters. Unnamed Parameters Unnamed parameters are automatically assigned numbers corresponding with the parameter position in the call. They are assigned }, } etc. and will represent the first, second etc. unnamed parameters. These are sometimes known as positional parameters. In our example above, MediaWiki assigns the word first to } and the word second to } because they are unnamed parameters. Named Parameters Instead of letting MediaWiki automatically assign the variable, you can name them so they are consistent. Named parameters give the template more consistency because the value should always be what you expect it to be. Instead of relying on adding parameters in the correct order, a requirement with unnamed parameters, assigning a name means that there is more flexibility in how the template is called. Users do not have to add the information in a specific order for it to render correctly. Named parameters must have a unique name within each template. To give a parameter a name, assign the name when adding the parameter, . This calls the template example with the named parameter color with the value red. Default Values Any parameter can have a default value. A default value is the value that is used if the parameter doesn't exist (or wasn't included in the parameter call). To specify a default value in the template, assign it when checking the parameter. To assign the default value of red to the } parameter, use } in the template's code. If the template is called with color undefined, the default color of red will be used. Mix and Match Parameters can take multiple named and unnamed parameters and deals with each one in order. For example, if the template is called like the following are assigned: } = red, } = blue, and } = orange. Substitution Substituting a template is the same as copy/pasting the template code onto the page. A substituted template is copied verbatim from the template page when the page is saved. Substitution has its advantages and disadvantages though. To substitute a template, add the prefix "subst:" to the template like . ;Advantages *Faster page load - Every template call slows down the page load slightly so substituting templates onto the page instead speeds up the page load. *A substituted template won't update when the template itself is updated. This is helpful for templates that are time-based and should not update when the template does. *Substituted templates can be easily edited on the page itself in the future. No need to track down the template to edit it for changes to show on the page. ;Disadvantages *No updates - if the template is to change in the future, substituting the template on the page will keep the template from auto-updating when the template page is changed. *Verbatim copy - Everything is copied from the template page when substituting. This includes all the code itself. Normally, parser functions are evaluated 'behind the scenes' and only the results are shown on the page. When substituting a template with these functions, these functions will still show the results, but the code itself is still on the page and is evaluated on each page load. For example, if the template has the code } + } }} and is substituted like , the expression code, , will be saved on the page instead of 12 as expected.